// pages/my/taxiRecord/taxiRecord.ts
// @ts-ignore
import { getOrderList } from '../../../http/api/taxiRecordService'
import { cancelAnOrder, payOrder, cancelAnReOrder } from '../../../http/api/orderService'
import { throttle } from '../../../utils/util'

const moment = require('../../../utils/moment')

const tokenMixin = require('../../../mixin/tokenMixin')

Page({
    behaviors: [tokenMixin],
    /**
     * 页面的初始数据
     */
    data: {
        typeMap: {
            '-2': '订单关闭',
            '-1': '未支付',
            '0': '未接单',
            '1': '已接单',
            '2': '已到达起始地点',
            '3': '配送途中',
            '4': '已到达终止地点',
            '5': '已结算'
        },
        typeTabMap: {
            订单关闭: '',
            未支付: 'warning',
            未接单: 'primary',
            已接单: 'success',
            已到达起始地点: 'success',
            配送途中: 'success',
            已到达终止地点: 'success',
            已结算: 'success'
        },
        taxiList: [],
        valueArr: [
            {
                label: '已接单',
                value: 1
            },
            {
                label: '待配送',
                value: 2
            },
            {
                label: '未支付',
                value: -1
            },
            {
                label: '未接单',
                value: 0
            },
            {
                label: '配送中',
                value: 3
            },
            {
                label: '已送达',
                value: 4
            },
            {
                label: '订单关闭',
                value: -2
            }
        ],
        value: 1,
        page: 1,
        pageSize: 5,
        total: 0
    },

    toMap(e: any) {
        let userLocation = false
        let userLocationBackground = false
        wx.getSetting({
            success: async ress => {
                for (let i in ress.authSetting) {
                    if (i == 'scope.userLocation' && ress.authSetting[i]) {
                        userLocation = ress.authSetting[i]
                    }
                    if (i == 'scope.userLocationBackground' && ress.authSetting[i]) {
                        userLocationBackground = ress.authSetting[i]
                    }
                }
                console.log(userLocation)
                console.log(userLocationBackground)
                if (userLocation && userLocationBackground) {
                    const orderinfo = e.currentTarget.dataset.orderinfo
                    console.log(orderinfo, e.currentTarget.dataset, '---orderinfo')
                    wx.navigateTo({
                        url: `/pages/map/map?orderInfo=${JSON.stringify(orderinfo)}&orderId=${orderinfo.ID}`
                    })
                } else {
                    wx.showToast({
                        title: '点击右上角设置\r\n开打后台定位',
                        icon: 'error',
                        duration: 2000
                    })
                }
            },
            fail: res => {
                console.log(res)
            }
        })
    },
    pay: throttle(async function (e: any) {
        const id = e.currentTarget.dataset.id
        const res: any = await payOrder(id)
        const tis = this
        console.log(res[0])
        wx.requestPayment({
            timeStamp: res[0].timeStamp.toString(),
            nonceStr: res[0].nonce,
            package: res[0].package,
            signType: res[0].signType,
            paySign: res[0].paySign,
            async success(res) {
                wx.showToast({
                    title: '支付成功',
                    icon: 'success',
                    duration: 1000
                })
                tis.onChange(2)
            },
            fail(res) {
                console.log(res)
                wx.showToast({
                    title: '支付失败',
                    icon: 'error',
                    duration: 500
                })
            }
        })
    }),

    async cancelOrder(e: any) {
        const id = e.currentTarget.dataset.id
        const type = e.currentTarget.dataset.type
        const money = e.currentTarget.dataset.order_money
        wx.navigateTo({
            url: '/pages/my/refund/refund?orderId=' + id + '&type=' + type + '&money=' + money
        })

        // await cancelAnOrder(id)
        // this.getList()
        // const res = await this.getList()
        // console.log(res)
        // this.setData({
        //     taxiList: res
        // })
    },
    cancelReOrder: throttle(async function (this: any, e: any) {
        const id = e.currentTarget.dataset.id
        const type = e.currentTarget.dataset.type
        const money = e.currentTarget.dataset.order_money

        wx.navigateTo({
            url: '/pages/my/refund/refund?orderId=' + id + '&type=' + type + '&money=' + money
        })
        // await cancelAnReOrder(id, order_money)
        // this.getList()
        // const res = await this.getList()
        // console.log(res)
        // this.setData({
        //     taxiList: res
        // })
    }),

    async onChange(e: any) {
        this.setData({
            value: e.detail.value,
            page: 1,
            total: 0
        })
        const res = await this.getList()
        console.log(res, '-----onChange')
        this.setData({
            taxiList: res
        })
    },

    async getList() {
        const { value, page, pageSize } = this.data
        const data: any = await getOrderList(value, page, pageSize)
        let arr: any = []
        if (data.data) {
            data.data.forEach((item: any) => {
                arr.push({
                    ...item,
                    order_time: moment.formatTime(item.order_time, 'YYYY-MM-DD hh:mm:ss')
                })
            })
        }
        this.setData({
            total: data.count
        })

        return arr
    },

    /**
     * 生命周期函数--监听页面加载
     */ async onLoad() {
        // const res = await this.getList()
        // console.log(res)
        // this.setData({
        //     // @ts-ignore
        //     taxiList: [...this.data.taxiList, ...res]
        // })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */ async onShow() {
        this.setData({
            page: 1
        })
        const res = await this.getList()
        await this.setData({
            // @ts-ignore
            taxiList: res
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */ async onPullDownRefresh() {
        this.setData({
            page: 1
        })
        const res = await this.getList()
        await this.setData({
            // @ts-ignore
            taxiList: res
        })
        //停止下拉刷新
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */ async onReachBottom() {
        if (this.data.page * this.data.pageSize >= this.data.total) {
            wx.showToast({
                title: '到底啦～'
            })
            return
        }
        const page = this.data.page + 1
        await this.setData({
            page: page
        })
        const res = await this.getList()
        console.log(this.data.page, res)
        this.setData({
            // @ts-ignore
            taxiList: [...this.data.taxiList, ...res]
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {}
})
